﻿#region

using System.Collections.Generic;
using System.Web.Script.Serialization;

#endregion

namespace HouseHoldBudget.Framework.Mvc.Helpers
{
    public class JsSerializer
    {
        #region Instance Methods

        /// <summary>
        ///   Deserializes the specified json string.
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="jsonString"> The json string. </param>
        /// <returns> </returns>
        public T Deserialize<T>(string jsonString)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Deserialize<T>(jsonString);
        }

        /// <summary>
        ///   Deserializes to array.
        /// </summary>
        /// <param name="jsonString"> The json string. </param>
        /// <returns> </returns>
        public List<JavaScriptArrayItem> DeserializeToArray(string jsonString)
        {
            return DeserializeToList<JavaScriptArrayItem>(jsonString);
        }

        /// <summary>
        ///   Deserializes jsonString to list of T.
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="jsonString"> The json string. </param>
        /// <returns> </returns>
        public List<T> DeserializeToList<T>(string jsonString)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Deserialize<List<T>>(jsonString);
        }

        #endregion
    }

    public class JavaScriptArrayItem
    {
        #region Instance Properties

        public string Name { get; set; }
        public string Value { get; set; }

        #endregion
    }
}